package com.cj.web.before;

import com.cj.entity.Course;
import com.cj.entity.PageBean;
import com.cj.service.CourseService;
import com.cj.utils.JSONUtils;
import com.cj.web.BaseServlet;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;

/**
 * @Auther: chenjie
 * @Date: 2021/7/13 - 07 - 13 - 15:41
 * @Description: com.cj.web.before
 */
@WebServlet("/courseWeb")
public class CourseServlet extends BaseServlet {
    CourseService service = new CourseService();

    public void findCourseByType(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String type = req.getParameter("type");

        List<Course> list =  service.findCourseByType(type);

        JSONUtils.objToJson(list,resp);
    }

    public void findPages(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String currentPage = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");
        String search = req.getParameter("search");
        String type = req.getParameter("courseType");

        PageBean<Course> pb=service.findPage(currentPage,pageSize,search,type);

        JSONUtils.objToJson(pb,resp);
    }


    public void findCourse(HttpServletRequest req, HttpServletResponse resp) throws IOException, ParseException {
//
        String cid = req.getParameter("cid");
//course 中添加了 存放coursedetail数据字段
        Course course = service.findCourse(cid);



        JSONUtils.objToJson(course,resp);
    }

//    查看这个用户购买的所有课
    public void findCourseByUser(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        String uid = req.getParameter("uid");
        List<Course> courseList = service.findCourseByUser(uid);


        ObjectMapper mapper = new ObjectMapper();
        String s = mapper.writeValueAsString(courseList);
        resp.getWriter().print(s);
    }
}
